package com.kaifamiao.wendao.dao;

import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.entity.Topic;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class CustomerDao extends BaseDao<Customer,Long> {

    private static final String SELECT_ALL="SELECT id,username,password,salt,reg_date,nickname FROM t_customers ORDER BY id";
    private static final String SELECT_MAX_ID = "SELECT max(id) from t_customers";
    private static final String INSERT_ONE = "INSERT INTO t_customers (id, username, password, salt, reg_date, nickname ) VALUES (?,?,?,?,?,?)";
    private static final String UPDATE_ONE = "UPDATE t_customers SET username=?, password=?, nickname=? WHERE id =?";
    private static final String SELECT_ONE = "SELECT id,username,password,salt,reg_date,nickname FROM t_customers WHERE id=?";
    private static final String DELETE_ONE = "DELETE FROM t_customers WHERE id = ?";
    private static final String SELECT_USERNAME = "SELECT id, username, password, salt, nickname, reg_date FROM t_customers WHERE username=?";
    private static final String SELECT_ONE_USERNAME = "SELECT id,username,password,salt,reg_date,nickname FROM t_customers WHERE username=?";


    @Override
    public boolean save(Customer c) {
        LocalDate date = c.getRegisterDate();
        java.sql.Date regDate = date != null ? java.sql.Date.valueOf(date) : null;
        Long maxId = null;
        try {
            maxId = runner.query(SELECT_MAX_ID, rs -> rs.next() ? rs.getLong(1) : null );
            c.setId((maxId == null ? 1 : (maxId+1)));
        } catch (SQLException e) {
            throw new RuntimeException("获取最大id时发生错误",e);
        }
        Object[] params = {c.getId(),c.getUsername(),c.getPassword(),c.getSalt(),regDate,c.getNickname()};

        try {
            return runner.update(INSERT_ONE,params) == 1;
        } catch (SQLException e) {
            throw new RuntimeException("保存失败",e);
        }

    }

    @Override
    public boolean update(Customer c) {
        Object[] params = {c.getUsername(),c.getPassword(),c.getNickname(),c.getId()};
        try {
            return runner.update(UPDATE_ONE, params) == 1 ;
        } catch (SQLException e) {
            throw new RuntimeException("更新失败", e);
        }
    }

    @Override
    public boolean remove(Long id) {
        try {
            return runner.update(DELETE_ONE,id) == 1;
        } catch (SQLException e) {
            throw new RuntimeException("删除失败",e);
        }
    }

    @Override
    public List<Customer> findAll() {
        ResultSetHandler<List<Customer>> rsHandler = rs -> {
            List<Customer> list = new ArrayList<>();
            while(rs.next()){
                Customer c = wrap(rs);
                list.add(c);
            }
            return list;

        };
        try {
            return runner.query(SELECT_ALL,rsHandler);
        } catch (SQLException e) {
            throw new RuntimeException("查询失败",e);
        }
    }

    private Customer wrap(ResultSet rs) throws SQLException {
        Customer c = new Customer();
        c.setId(rs.getLong("id"));
        c.setUsername(rs.getString("username"));
        c.setPassword(rs.getString("password"));

        java.sql.Date date = rs.getDate("reg_date");
        LocalDate regDate = date == null ? null : date.toLocalDate();
        c.setRegisterDate(regDate);

        c.setNickname(rs.getString("nickname"));

        return c;
    }

    @Override
    public Customer find(Long id) {
        ResultSetHandler<Customer> rsHandler = rs -> {
            if(rs.next()){
                Customer c = wrap(rs);
                return c;
            }
            return null;
        };
        try {
            return runner.query(SELECT_ONE,rsHandler,id);
        } catch (SQLException e) {
            throw new RuntimeException("查找失败",e);
        }

    }

    //当前用户所发表的话题
//    public List<Topic> topicsFind(Long id){
//        ResultSetHandler<List<Topic>> rsHandler = rs -> {
//            List<Topic> list = new ArrayList<>();
//            while (rs.next()){
//               Customer c = new Customer();
//               Topic t = new Topic();
//               c.setTopics(list);
//
//            }
//            return list;
//        };
//
//        try {
//            return runner.query(SELECT_TOPIC,rsHandler);
//        } catch (SQLException e) {
//            throw new RuntimeException("查询话题失败",e);
//        }
//
//    }

    public boolean findUname(String  username){

        try {
            return  runner.query(SELECT_USERNAME,rs -> rs.next(),username) ;
        } catch (SQLException e) {
            throw new RuntimeException("查找用户名失败",e);
        }

    }

    public Customer find(String username) {
        try {
            return runner.query(SELECT_USERNAME, singleHandler, username);
        }catch (SQLException cause){
            throw new RuntimeException("查询失败",cause);
        }
    }

    private ResultSetHandler<Customer> singleHandler = rs -> {
        Customer c = null;
        if(rs.next()){
            c = new Customer();
            c.setId(rs.getLong("id"));
            c.setUsername(rs.getString("username"));
            c.setPassword(rs.getString("password"));
            c.setSalt(rs.getString("salt"));
            c.setNickname(rs.getString("nickname"));
            java.sql.Date date = rs.getDate("reg_date");
            LocalDate regDate = date == null ? null : date.toLocalDate();
            c.setRegisterDate(regDate);
        }
        return c;
    };

    }

